[MFC] I can't re-parent a window

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-04-28T15:53:08Z Indexed on 2010/04/29 9:07 UTC
Read the original article Hit count: 591

Filed under:
|

Following on from this question, now I have a clearer picture what's going on...

I have a MFC application with no main window, which exposes an API to create dialogs. When I call some of these methods repeatedly, the dialogs created are parented to each other instead of all being parented to the desktop... I have no idea why.

But anyway even after creation, I am unable to change the parent back to NULL or CWnd::GetDesktopWindow()... if I call SetParent followed by GetParent, nothing has changed.

So apart from the really weird question of why Windows is magically parenting each dialog to the last one created, is there anything I'm missing to be able to set these windows as children of the desktop?


UPDATED: I have found the reason for all this, but not the solution. From my dialog constructor, we end up in:

BOOL CDialog::CreateIndirect(LPCDLGTEMPLATE lpDialogTemplate, CWnd* pParentWnd,
    void* lpDialogInit, HINSTANCE hInst)
{
    ASSERT(lpDialogTemplate != NULL);

    if (pParentWnd == NULL)
        pParentWnd = AfxGetMainWnd();
    m_lpDialogInit = lpDialogInit;

    return CreateDlgIndirect(lpDialogTemplate, pParentWnd, hInst);
}

Note: if (pParentWnd == NULL)pParentWnd = AfxGetMainWnd();

The call-stack from my dialog constructor looks like this:

  • mfc80d.dll!CDialog::CreateIndirect(const DLGTEMPLATE * lpDialogTemplate=0x005931a8, CWnd * pParentWnd=0x00000000, void * lpDialogInit=0x00000000, HINSTANCE__ * hInst=0x00400000)
  • mfc80d.dll!CDialog::CreateIndirect(void * hDialogTemplate=0x005931a8, CWnd * pParentWnd=0x00000000, HINSTANCE__ * hInst=0x00400000)
  • mfc80d.dll!CDialog::Create(const char * lpszTemplateName=0x0000009d, CWnd * pParentWnd=0x00000000)
  • mfc80d.dll!CDialog::Create(unsigned int nIDTemplate=157, CWnd * pParentWnd=0x00000000)
  • MyApp.exe!CMyDlg::CMyDlg(CWnd * pParent=0x00000000)

© Stack Overflow or respective owner

Related posts about mfc

Related posts about win32